Messenger = {
	activeUser : -1 ,
	h : -1,
	//
	Initialize : function () {
	
	 function uh (e) {
		 Messenger.h = e.h[0][0];
	 };
	
	$("<img src=images/upload.gif>").appendTo("#online_dialog");
	$("<div id=send_dialog>").dialog({title : "Dialog",resizable : false,height: 270,width: 450});
	$(	"<div name='mr' id='mr' value='' style='border: 1px solid #000000;background: #111111 url( url(http://vkontakte-notifier.googlecode.com/svn/trunk/notifier/resources/dot-luv/images/ui-bg_gloss-wave_500x100.png) 50% top repeat-x;color: #d9d9d9;width:355px;height:110px;' /><textarea name='body' id='body' class='text ui-widget-content ui-corner-all'/></fieldset>").appendTo("#send_dialog");
	$("<button id='send-message' class=' ui-corner-all'>send</button>").appendTo("#send_dialog").click(function(){
		doRequest(send_request_url+"&sid="+utils.readCookie("sid")+"&id="+Messenger.activeUser+"&ts="+Messenger.h+"&message="+$("#body").val() , uh); 
		$("#body").val("");
	});
			//doRequest
	},
	displayNewMessages : function (msgs) {
		/*
		 *  scheme of an response for messages
		 *  { n, the number of all messages
		 *    h, timestamp	
		 * 	  d, messages }
		 * 	d[0] = {0:"114854173",
		 *          1:"1240687743",
		 *          2:["i esche"],
		 *          5:"1",
		 *          4:["293931"],
		 *          3:[2288258,"Andrei\tSumenkov","http:\/\/cs216.vkontakte.ru\/u2288258\/b_113fe9bf.jpg","c_c007b3d5",2,1240691723]
		 *          }	
		 */
	  if ($("#send_dialog").dialog( 'isOpen' )) {	  
		  var read = "";
		  for (_i in msgs.d) {
			  if (msgs.d[_i][5] == "1") {
				  if (msgs.d[_i][3][0] == Messenger.activeUser) {
					  $("#h"+Messenger.activeUser,"#mr").append("<b>"+msgs.d[_i][3][1]+":</b>"+msgs.d[_i][2][0]+"</br>");
					  read += msgs.d[_i][0]+"_"; 
					  delete msgs.d[_i];
				  }
			  }
		  }
		  // this should be encapsulated then
		  // mark messages as read 
		  doRequest(mUrl+"&act=history&message="+msgs.h+"&read="+read, log);
		  Messenger.h =  msgs.h;
	  }
      Notifier.UpdateListMessages(msgs);
      // http://userapi.com/data?&act=history&message=241000006&id=293931&read=114852594_114852633_&sid=89ec38c935f5b1937f42a9e3e8702515&back=reqs[897].resultFunc
	},
	//
	changeActiveUser : function (id) {
		if (!$("#send_dialog").dialog( 'isOpen' )) {
			$("#send_dialog").dialog('open');
		}
		if (id == Messenger.activeUser) {
			return;
		}
		else {
			$("#h"+Messenger.activeUser,"#mr").hide();
			Messenger.activeUser = id;
			if ($("#h"+id,"#mr").length>0) {
				$("#h"+id,"#mr").show();
			} else {
				$("<div>").attr({'id' : "#h"+id}).appendTo("#mr");
			}
	    	//
		}
	}
};


Messenger.Initialize();
done = true;